<div class="tmd-doc">
<h1 class="tmd-header-1">
Basic Control Flows
</h1>
<p></p>
<div class="tmd-usual">
The control flow code blocks in Go are much like other popular programming languages, but there are also many differences. This article will show these similarities and differences.
</div>
<p></p>
<h3 class="tmd-header-3">
An Introduction of Control Flows in Go
</h3>
<p></p>
<div class="tmd-usual">
There are three kinds of basic control flow code blocks in Go:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">if-else</code> two-way conditional execution block.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">for</code> loop block.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">switch-case</code> multi-way conditional execution block.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
There are also some control flow code blocks which are related to some certain kinds of types in Go.
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">for-range</code> loop block to iterate integers, all kinds of <a href="container.html#iteration">containers</a>, <a href="channel.html#range">channels</a>, and <a href="function.html#range">some functions</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">type-switch</code> multi-way conditional execution block for <a href="interface.html#type-switch">interfaces</a>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">select-case</code> block for <a href="channel.html#select">channels</a>.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
Like many other popular languages, Go also supports <code class="tmd-code-span">break</code>, <code class="tmd-code-span">continue</code> and <code class="tmd-code-span">goto</code> code execution jump statements. Besides these, there is a special code jump statement in Go, <code class="tmd-code-span">fallthrough</code>.
</div>
<p></p>
<div class="tmd-usual">
Among the six kinds of control flow blocks, except the <code class="tmd-code-span">if-else</code> control flow, the other five are called <span class="tmd-bold">breakable control flow blocks</span>. We can use <code class="tmd-code-span">break</code> statements to make executions jump out of breakable control flow blocks.
</div>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">for</code> and <code class="tmd-code-span">for-range</code> loop blocks are called <span class="tmd-bold">loop control flow blocks</span>. We can use <code class="tmd-code-span">continue</code> statements to end a loop iteration in advance in a loop control flow block, i.e. continue to the next iteration of the loop.
</div>
<p></p>
<div class="tmd-usual">
Please note, each of the above mentioned control flow blocks is a statement, and it may contain many other sub-statements.
</div>
<p></p>
<div class="tmd-usual">
Above mentioned control flow statements are all the ones in narrow sense. The mechanisms introduced in the next article, <a href="control-flows-more.html">goroutines, deferred function calls and panic/recover</a>, and the concurrency synchronization techniques introduced in the later article <a href="concurrent-synchronization-overview.html">concurrency synchronization overview</a> can be viewed as control flow statements in broad sense.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
The current article mainly explains the basic control flow code blocks and code jump statements. Go 1.22 introduced <code class="tmd-code-span">for range anInteger {...}</code> loops will be also touched. Other control flow code blocks will be explained in many other coming Go 101 articles.
</div>
<p></p>
<h3 id="if-else" class="tmd-header-3">
<code class="tmd-code-span">if-else</code> Control Flow Blocks
</h3>
<p></p>
<div class="tmd-usual">
The full form of a <code class="tmd-code-span">if-else</code> code block is like
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">if InitSimpleStatement; Condition {
	// do something
} else {
	// do something
}
</code></pre>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">if</code> and <code class="tmd-code-span">else</code> are keywords. Like many other programming languages, the <code class="tmd-code-span">else</code> branch is optional.
</div>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">InitSimpleStatement</code> portion is also optional. It must be a <a href="expressions-and-statements.html#simple-statements">simple statement</a> if it is present. If it is absent, we can view it as a blank statement (one kind of simple statements). In practice, <code class="tmd-code-span">InitSimpleStatement</code> is often a short variable declaration or a pure assignment. A <code class="tmd-code-span">Condition</code> must be an <a href="expressions-and-statements.html#expressions">expression</a> which results to a boolean value. The <code class="tmd-code-span">Condition</code> portion can be enclosed in a pair of <code class="tmd-code-span">()</code> or not, but it can't be enclosed together with the <code class="tmd-code-span">InitSimpleStatement</code> portion.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
If the <code class="tmd-code-span">InitSimpleStatement</code> in a <code class="tmd-code-span">if-else</code> block is present, it will be executed before executing other statements in the <code class="tmd-code-span">if-else</code> block. If the <code class="tmd-code-span">InitSimpleStatement</code> is absent, then the semicolon following it is optional.
</div>
<p></p>
<div class="tmd-usual">
Each <code class="tmd-code-span">if-else</code> control flow forms one implicit code block, one <code class="tmd-code-span">if</code> branch explicit code block and one optional <code class="tmd-code-span">else</code> branch code block. The two branch code blocks are both nested in the implicit code block. Upon execution, if <code class="tmd-code-span">Condition</code> expression results in <code class="tmd-code-span">true</code>, then the <code class="tmd-code-span">if</code> branch block will get executed, otherwise, the <code class="tmd-code-span">else</code> branch block will get executed.
</div>
<p></p>
<div class="tmd-usual">
Example:
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano()) // needed before Go 1.20

	if n := rand.Int(); n%2 == 0 {
		fmt.Println(n, "is an even number.")
	} else {
		fmt.Println(n, "is an odd number.")
	}

	n := rand.Int() % 2 // this n is not the above n.
	if n % 2 == 0 {
		fmt.Println("An even number.")
	}

	if ; n % 2 != 0 {
		fmt.Println("An odd number.")
	}
}
</code></pre>
<p></p>
<div class="tmd-usual">
If the <code class="tmd-code-span">InitSimpleStatement</code> in a <code class="tmd-code-span">if-else</code> code block is a short variable declaration, then the declared variables will be viewed as being declared in the top nesting implicit code block of the <code class="tmd-code-span">if-else</code> code block.
</div>
<p></p>
<div class="tmd-usual">
An <code class="tmd-code-span">else</code> branch code block can be implicit if the corresponding <code class="tmd-code-span">else</code> is followed by another <code class="tmd-code-span">if-else</code> code block, otherwise, it must be explicit.
</div>
<p></p>
<div class="tmd-usual">
Example:
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import (
	"fmt"
	"time"
)

func main() {
	if h := time.Now().Hour(); h &lt; 12 {
		fmt.Println("Now is AM time.")
	} else if h &gt; 19 {
		fmt.Println("Now is evening time.")
	} else {
		fmt.Println("Now is afternoon time.")
		h := h // the right one is declared above
		// The just new declared "h" variable
		// shadows the above same-name one.
		_ = h
	}

	// h is not visible here.
}
</code></pre>
<p></p>
<h3 id="for" class="tmd-header-3">
<code class="tmd-code-span">for</code> Loop Control Flow Blocks
</h3>
<p></p>
<div class="tmd-usual">
The full form of a <code class="tmd-code-span">for</code> loop block is:
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">for InitSimpleStatement; Condition; PostSimpleStatement {
	// do something
}
</code></pre>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">for</code> is a keyword. The <code class="tmd-code-span">InitSimpleStatement</code> and <code class="tmd-code-span">PostSimpleStatement</code> portions must be both simple statements, and the <code class="tmd-code-span">PostSimpleStatement</code> portion must not be a short variable declaration. <code class="tmd-code-span">Condition</code> must be an expression which result is a boolean value. The three portions are all optional.
</div>
<p></p>
<div class="tmd-usual">
Unlike many other programming languages, the just mentioned three parts following the <code class="tmd-code-span">for</code> keyword can't be enclosed in a pair of <code class="tmd-code-span">()</code>.
</div>
<p></p>
<div class="tmd-usual">
Each <code class="tmd-code-span">for</code> control flow forms at least two code blocks, one is implicit and one is explicit. The explicit one is nested in the implicit one.
</div>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">InitSimpleStatement</code> in a <code class="tmd-code-span">for</code> loop block will be executed (only once) before executing other statements in the <code class="tmd-code-span">for</code> loop block.
</div>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">Condition</code> expression will be evaluated at each loop iteration. If the evaluation result is <code class="tmd-code-span">false</code>, then the loop will end. Otherwise the body (a.k.a., the explicit code block) of the loop will get executed.
</div>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">PostSimpleStatement</code> will be executed at the end of each loop iteration.
</div>
<p></p>
<div class="tmd-usual">
A <code class="tmd-code-span">for</code> loop example. The example will print the integers from <code class="tmd-code-span">0</code> to <code class="tmd-code-span">9</code>.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := 0; i &lt; 10; i++ {
	fmt.Println(i)
}
</code></pre>
<p></p>
<div class="tmd-usual">
If the <code class="tmd-code-span">InitSimpleStatement</code> and <code class="tmd-code-span">PostSimpleStatement</code> portions are both absent (just view them as blank statements), their nearby two semicolons can be omitted. The form is called as condition-only <code class="tmd-code-span">for</code> loop form. It is the same as the <code class="tmd-code-span">while</code> loop in other languages.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">var i = 0
for ; i &lt; 10; {
	fmt.Println(i)
	i++
}
for i &lt; 20 {
	fmt.Println(i)
	i++
}
</code></pre>
<p></p>
<div class="tmd-usual">
If the <code class="tmd-code-span">Condition</code> portion is absent, compilers will view it as <code class="tmd-code-span">true</code>.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := 0; ; i++ { // &lt;=&gt; for i := 0; true; i++ {
	if i &gt;= 10 {
		// "break" statement will be explained below.
		break
	}
	fmt.Println(i)
}

// The following 4 endless loops are
// equivalent to each other (for most cases).
for ; true; {
}
for true {
}
for ; ; {
}
for {
}
</code></pre>
<p></p>
<div class="tmd-usual">
If the <code class="tmd-code-span">InitSimpleStatement</code> in a <code class="tmd-code-span">for</code> block is a short variable declaration statement, then the declared loop variables will be viewed as being declared in the top nesting implicit code block of the <code class="tmd-code-span">for</code> block. For example, the following code snippet prints <code class="tmd-code-span">012</code> instead of <code class="tmd-code-span">0</code>.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := 0; i &lt; 3; i++ {
	fmt.Print(i)
	// The left i is a new declared variable,
	// and the right i is the loop variable.
	i := i
	// The new declared variable is modified, but
	// the old one (the loop variable) is not yet.
	i = 10
	_ = i
}
</code></pre>
<p></p>
<div id="for-semantic-change" class="tmd-usual">
Note: Go 1.22 modified the semantics of <code class="tmd-code-span">for</code> loop blocks.
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
Prior to Go 1.22, every declared loop variable used in a <code class="tmd-code-span">for</code> loop block was shared by all iterations during executing the loop block.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
Since Go 1.22, every declared loop variable used in a <code class="tmd-code-span">for</code> loop will be instantiated as a distinctive instance at the start of each iteration.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
For most cases, the semantic change doesn't change code behavior. But <a href="control-flows-more.html#argument-evaluation-moment">sometimes, it does</a>. So the semantic change breaks backward compatibility. Since Go 1.22, <a href="https://go101.org/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.html">every Go source file should be specified a Go version</a> to reduce the damage as small as possible.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
A <code class="tmd-code-span">break</code> statement can be used to make execution jump out of a <code class="tmd-code-span">for</code> loop control flow block in advance, if the <code class="tmd-code-span">for</code> loop control flow block is the innermost breakable control flow block containing the <code class="tmd-code-span">break</code> statement. For example, the following code also prints <code class="tmd-code-span">0</code> to <code class="tmd-code-span">9</code>.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">i := 0
for {
	if i &gt;= 10 {
		break
	}
	fmt.Println(i)
	i++
}
</code></pre>
<p></p>
<div class="tmd-usual">
A <code class="tmd-code-span">continue</code> statement can be used to end the current loop iteration in advance (<code class="tmd-code-span">PostSimpleStatement</code> will still get executed), if the <code class="tmd-code-span">for</code> loop control flow block is the innermost loop control flow block containing the <code class="tmd-code-span">continue</code> statement. For example, the following code snippet will print <code class="tmd-code-span">13579</code>.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := 0; i &lt; 10; i++ {
	if i % 2 == 0 {
		continue
	}
	fmt.Print(i)
}
</code></pre>
<p></p>
<h3 id="for-range-integer" class="tmd-header-3">
Use <code class="tmd-code-span">for-range</code> Control Flow Blocks to Iterate Integers
</h3>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">for-range</code> loop blocks can be used to iterate integers, all kinds of <a href="container.html#iteration">containers</a>, <a href="channel.html#rang">channels</a>, and <a href="function.html#range">some functions</a>.. The current article only explains how to use <code class="tmd-code-span">for-range</code> loop blocks to iterate integers.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
Note: using <code class="tmd-code-span">for-range</code> loop blocks to iterate integers is only supported since Go 1.22.
</div>
<p></p>
<div class="tmd-usual">
The following code
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">// i is declared earlier.
for i = range anInteger {
	...
}
</code></pre>
<p></p>
<div class="tmd-usual">
is actually a short form of
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">for i = 0; i &lt; anInteger; i++ {
	...
}
</code></pre>
<p></p>
<div class="tmd-usual">
Similarly,
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := range anInteger {
	...
}
</code></pre>
<p></p>
<div class="tmd-usual">
is actually a short form of
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := 0; i &lt; anInteger; i++ {
	...
}
</code></pre>
<p></p>
<div class="tmd-usual">
with Go 1.22+ semantics.
</div>
<p></p>
<div class="tmd-usual">
For example, the last example in the last section is equivalent to
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">for i := range 10 {
	if i % 2 == 0 {
		continue
	}
	fmt.Print(i)
}
</code></pre>
<p></p>
<h3 id="switch-case" class="tmd-header-3">
<code class="tmd-code-span">switch-case</code> Control Flow Blocks
</h3>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">switch-case</code> control flow block is one kind of conditional execution control flow blocks.
</div>
<p></p>
<div class="tmd-usual">
The full form a <code class="tmd-code-span">switch-case</code> block is
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">switch InitSimpleStatement; CompareOperand0 {
case CompareOperandList1:
	// do something
case CompareOperandList2:
	// do something
...
case CompareOperandListN:
	// do something
default:
	// do something
}
</code></pre>
<p></p>
<div class="tmd-usual">
In the full form,
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">switch</code>, <code class="tmd-code-span">case</code> and <code class="tmd-code-span">default</code> are three keywords.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
The <code class="tmd-code-span">InitSimpleStatement</code> portion must be a simple statement. The <code class="tmd-code-span">CompareOperand0</code> portion is an expression which is viewed as a typed value (if it is an untyped value, then it is viewed as a type value of its default type), hence it can't be an untyped <code class="tmd-code-span">nil</code>. <code class="tmd-code-span">CompareOperand0</code> is called as switch expression in Go specification.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
Each of the <code class="tmd-code-span">CompareOperandListX</code> (<code class="tmd-code-span">X</code> may represent from <code class="tmd-code-span">1</code> to <code class="tmd-code-span">N</code>) portions must be a comma separated expression list. Each of these expressions shall be comparable with <code class="tmd-code-span">CompareOperand0</code>. Each of these expressions is called as a case expression in Go specification. If a case expression is an untyped value, then it must be implicitly convertible to the type of the switch expression in the same <code class="tmd-code-span">switch-case</code> control flow. If the conversion is impossible to achieve, compilation fails.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
Each <code class="tmd-code-span">case CompareOperandListX:</code> or <code class="tmd-code-span">default:</code> opens (and is followed by) an implicit code block. The implicit code block and that <code class="tmd-code-span">case CompareOperandListX:</code> or <code class="tmd-code-span">default:</code> forms a branch. Each such branch is optional to be present. We call an implicit code block in such a branch as a branch code block later.
</div>
<p></p>
<div class="tmd-usual">
There can be at most one <code class="tmd-code-span">default</code> branch in a <code class="tmd-code-span">switch-case</code> control flow block.
</div>
<p></p>
<div class="tmd-usual">
Besides the branch code blocks, each <code class="tmd-code-span">switch-case</code> control flow forms two code blocks, one is implicit and one is explicit. The explicit one is nested in the implicit one. All the branch code blocks are nested in the explicit one (and nested in the implicit one indirectly).
</div>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">switch-case</code> control flow blocks are breakable, so <code class="tmd-code-span">break</code> statements can also be used in any branch code block in a <code class="tmd-code-span">switch-case</code> control flow block to make execution jump out of the <code class="tmd-code-span">switch-case</code> control flow block in advance.
</div>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">InitSimpleStatement</code> will get executed firstly when a <code class="tmd-code-span">switch-case</code> control flow gets executed. It will get executed only once during executing the <code class="tmd-code-span">switch-case</code> control flow. After the <code class="tmd-code-span">InitSimpleStatement</code> gets executed, the switch expression <code class="tmd-code-span">CompareOperand0</code> will be evaluated and only evaluated once. The evaluation result is always a typed value. The evaluation result will be compared (by using the <code class="tmd-code-span">==</code> operator) with the evaluation result of each case expression in the <code class="tmd-code-span">CompareOperandListX</code> expression lists, from top to down and from left to right. If a case expression is found to be equal to <code class="tmd-code-span">CompareOperand0</code>, the comparison process stops and the corresponding branch code block of the expression will be executed. If none case expressions are found to be equal to <code class="tmd-code-span">CompareOperand0</code>, the default branch code block (if it is present) will get executed.
</div>
<p></p>
<div class="tmd-usual">
A <code class="tmd-code-span">switch-case</code> control flow example:
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano()) // needed before Go 1.20
	switch n := rand.Intn(100); n%9 {
	case 0:
		fmt.Println(n, "is a multiple of 9.")

		// Different from many other languages,
		// in Go, the execution will automatically
		// jumps out of the switch-case block at
		// the end of each branch block.
		// No "break" statement is needed here.
	case 1, 2, 3:
		fmt.Println(n, "mod 9 is 1, 2 or 3.")
		// Here, this "break" statement is nonsense.
		break
	case 4, 5, 6:
		fmt.Println(n, "mod 9 is 4, 5 or 6.")
	// case 6, 7, 8:
		// The above case line might fail to compile,
		// for 6 is duplicate with the 6 in the last
		// case. The behavior is compiler dependent.
	default:
		fmt.Println(n, "mod 9 is 7 or 8.")
	}
}
</code></pre>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">rand.Intn</code> function returns a non-negative <code class="tmd-code-span">int</code> random value which is smaller than the specified argument.
</div>
<p></p>
<div class="tmd-usual">
Note, if any two case expressions in a <code class="tmd-code-span">switch-case</code> control flow can be detected to be equal at compile time, then a compiler may reject the latter one. For example, the standard Go compiler thinks the <code class="tmd-code-span">case 6, 7, 8</code> line in the above example is invalid if that line is not commented out. But other compilers may think that line is okay. In fact, the current standard Go compiler (version 1.25.n) <a href="https://github.com/golang/go/issues/28357">allows duplicate boolean case expressions</a>, and gccgo (v8.2) allows both duplicate boolean and string case expressions.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
As the comments in the above example describes, unlike many other languages, in Go, at the end of each branch code block, the execution will automatically break out of the corresponding <code class="tmd-code-span">switch-case</code> control block. Then how to let the execution slip into the next branch code block? Go provides a <code class="tmd-code-span">fallthrough</code> keyword to do this task. For example, in the following example, every branch code block will get executed, by their orders, from top to down.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">rand.Seed(time.Now().UnixNano()) // needed before Go 1.20
switch n := rand.Intn(100) % 5; n {
case 0, 1, 2, 3, 4:
	fmt.Println("n =", n)
	// The "fallthrough" statement makes the
	// execution slip into the next branch.
	fallthrough
case 5, 6, 7, 8:
	// A new declared variable also called "n",
	// it is only visible in the current
	// branch code block.
	n := 99
	fmt.Println("n =", n) // 99
	fallthrough
default:
	// This "n" is the switch expression "n".
	fmt.Println("n =", n)
}
</code></pre>
<p></p>
<div class="tmd-usual">
Please note,
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
a <code class="tmd-code-span">fallthrough</code> statement must be the final statement in a branch.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
a <code class="tmd-code-span">fallthrough</code> statement can't show up in the final branch in a <code class="tmd-code-span">switch-case</code> control flow block.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
For example, the following <code class="tmd-code-span">fallthrough</code> uses are all illegal.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">switch n := rand.Intn(100) % 5; n {
case 0, 1, 2, 3, 4:
	fmt.Println("n =", n)
	// The if-block, not the fallthrough statement,
	// is the final statement in this branch.
	if true {
		fallthrough // error: not the final statement
	}
case 5, 6, 7, 8:
	n := 99
	fallthrough // error: not the final statement
	_ = n
default:
	fmt.Println(n)
	fallthrough // error: show up in the final branch
}
</code></pre>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">InitSimpleStatement</code> and <code class="tmd-code-span">CompareOperand0</code> portions in a <code class="tmd-code-span">switch-case</code> control flow are both optional. If the <code class="tmd-code-span">CompareOperand0</code> portion is absent, it will be viewed as <code class="tmd-code-span">true</code>, a typed value of the built-in type <code class="tmd-code-span">bool</code>. If the <code class="tmd-code-span">InitSimpleStatement</code> portion is absent, the semicolon following it can be omitted.
</div>
<p></p>
<div class="tmd-usual">
And as above has mentioned, all branches are optional. So the following code blocks are all legal, all of them can be viewed as no-ops.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">switch n := 5; n {
}

switch 5 {
}

switch _ = 5; {
}

switch {
}
</code></pre>
<p></p>
<div class="tmd-usual">
For the latter two <code class="tmd-code-span">switch-case</code> control flow blocks in the last example, as above has mentioned, each of the absent <code class="tmd-code-span">CompareOperand0</code> portions is viewed as a typed value <code class="tmd-code-span">true</code> of the built-in type <code class="tmd-code-span">bool</code>. So the following code snippet will print <code class="tmd-code-span">hello</code>.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">switch { // &lt;=&gt; switch true {
case true: fmt.Println("hello")
default: fmt.Println("bye")
}
</code></pre>
<p></p>
<p id="default-branch-placement"></p>
<div class="tmd-usual">
Another obvious difference from many other languages is the order of the <code class="tmd-code-span">default</code> branch in a <code class="tmd-code-span">switch-case</code> control flow block can be arbitrary. For example, the following three <code class="tmd-code-span">switch-case</code> control flow blocks are equivalent to each other.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">switch n := rand.Intn(3); n {
case 0: fmt.Println("n == 0")
case 1: fmt.Println("n == 1")
default: fmt.Println("n == 2")
}

switch n := rand.Intn(3); n {
default: fmt.Println("n == 2")
case 0: fmt.Println("n == 0")
case 1: fmt.Println("n == 1")
}

switch n := rand.Intn(3); n {
case 0: fmt.Println("n == 0")
default: fmt.Println("n == 2")
case 1: fmt.Println("n == 1")
}
</code></pre>
<p></p>
<h3 class="tmd-header-3">
<code class="tmd-code-span">goto</code> Statement and Label Declaration
</h3>
<p></p>
<div class="tmd-usual">
Like many other languages, Go also supports <code class="tmd-code-span">goto</code> statement. A <code class="tmd-code-span">goto</code> keyword must be followed by a label to form a statement. A label is declared with the form <code class="tmd-code-span">LabelName:</code>, where <code class="tmd-code-span">LabelName</code> must be an identifier. A label which name is not the blank identifier must be used at least once.
</div>
<p></p>
<div class="tmd-usual">
A <code class="tmd-code-span">goto</code> statement will make the execution jump to the next statement following the declaration of the label used in the <code class="tmd-code-span">goto</code> statement. So a label declaration must be followed by one statement.
</div>
<p></p>
<div class="tmd-usual">
A label must be declared within a function body. A use of a label can appear before or after the declaration of the label. But a label is not visible (and can't appear) outside the innermost code block the label is declared in.
</div>
<p></p>
<div class="tmd-usual">
The following example uses a <code class="tmd-code-span">goto</code> statement and a label to implement a loop control flow.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "fmt"

func main() {
	i := 0

Next: // here, a label is declared.
	fmt.Println(i)
	i++
	if i &lt; 5 {
		goto Next // execution jumps
	}
}
</code></pre>
<p></p>
<div class="tmd-usual">
As mentioned above, a label is not visible (and can't appear) outside the innermost code block the label is declared in. So the following example fails to compile.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

func main() {
goto Label1 // error
	{
		Label1:
		goto Label2 // error
	}
	{
		Label2:
	}
}
</code></pre>
<p></p>
<div class="tmd-usual">
Note that, if a label is declared within the scope of a variable, then the uses of the label can't appear before the declaration of the variable. Identifier scopes will be explained in the article <a href="blocks-and-scopes.html">blocks and scopes in Go</a> later.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
The following example also fails to compile.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "fmt"

func main() {
	i := 0
Next:
	if i &gt;= 5 {
		// error: jumps over declaration of k
		goto Exit
	}

	k := i + i
	fmt.Println(k)
	i++
	goto Next

// This label is declared in the scope of k,
// but its use is outside of the scope of k.
Exit:
}
</code></pre>
<p></p>
<div class="tmd-usual">
The just mentioned rule <a href="https://github.com/golang/go/issues/26058">may change later</a>. Currently, to make the above code compile okay, we must adjust the scope of the variable <code class="tmd-code-span">k</code>. There are two ways to fix the problem in the last example.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
One way is to shrink the scope of the variable <code class="tmd-code-span">k</code>.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">func main() {
	i := 0
Next:
	if i &gt;= 5 {
		goto Exit
	}
	// Create an explicit code block to
	// shrink the scope of k.
	{
		k := i + i
		fmt.Println(k)
	}
	i++
	goto Next
Exit:
}
</code></pre>
<p></p>
<div class="tmd-usual">
The other way is to enlarge the scope of the variable <code class="tmd-code-span">k</code>.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">func main() {
	var k int // move the declaration of k here.
	i := 0
Next:
	if i &gt;= 5 {
		goto Exit
	}

	k = i + i
	fmt.Println(k)
	i++
	goto Next
Exit:
}
</code></pre>
<p></p>
<h3 class="tmd-header-3">
<code class="tmd-code-span">break</code> and <code class="tmd-code-span">continue</code> Statements With Labels
</h3>
<p></p>
<div class="tmd-usual">
A <code class="tmd-code-span">goto</code> statement must contain a label. A <code class="tmd-code-span">break</code> or <code class="tmd-code-span">continue</code> statement can also contain a label, but the label is optional. Generally, <code class="tmd-code-span">break</code> containing labels are used in nested breakable control flow blocks and <code class="tmd-code-span">continue</code> statements containing labels are used in nested loop control flow blocks.
</div>
<p></p>
<div class="tmd-usual">
If a <code class="tmd-code-span">break</code> statement contains a label, the label must be declared just before a breakable control flow block which contains the <code class="tmd-code-span">break</code> statement. We can view the label name as the name of the breakable control flow block. The <code class="tmd-code-span">break</code> statement will make execution jump out of the breakable control flow block, even if the breakable control flow block is not the innermost breakable control flow block containing <code class="tmd-code-span">break</code> statement.
</div>
<p></p>
<div class="tmd-usual">
If a <code class="tmd-code-span">continue</code> statement contains a label, the label must be declared just before a loop control flow block which contains the <code class="tmd-code-span">continue</code> statement. We can view the label name as the name of the loop control flow block. The <code class="tmd-code-span">continue</code> statement will end the current loop iteration of the loop control flow block in advance, even if the loop control flow block is not the innermost loop control flow block containing the <code class="tmd-code-span">continue</code> statement.
</div>
<p></p>
<div class="tmd-usual">
The following is an example of using <code class="tmd-code-span">break</code> and <code class="tmd-code-span">continue</code> statements with labels.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "fmt"

func FindSmallestPrimeLargerThan(n int) int {
Outer:
	for n++; ; n++{
		for i := 2; ; i++ {
			switch {
			case i * i &gt; n:
				break Outer
			case n % i == 0:
				continue Outer
			}
		}
	}
	return n
}

func main() {
	for i := 90; i &lt; 100; i++ {
		n := FindSmallestPrimeLargerThan(i)
		fmt.Print("The smallest prime number larger than ")
		fmt.Println(i, "is", n)
	}
}
</code></pre>
<p></p>
</div>
